/**
 *Copyright (c) 2024 watereyes
 * workflow is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.workflow.api.listener;

import java.util.Date;

import org.activiti.engine.delegate.event.ActivitiEvent;
import org.koron.ebs.mybatis.SessionFactory;

import com.koron.workflow.api.bean.HistoryBillsBean;
import com.koron.workflow.api.bean.SubHistoryBillsBean;
import com.koron.workflow.api.mapper.HistoryBillsMapper;
import com.koron.workflow.api.mapper.SubHistoryBillsMapper;
import com.koron.workflow.common.util.SessionFactoryLocal;

/**
 * 流程结束监听器
 * @author bin
 *
 */
public class ProcessCompletedListener extends ProcessListenerAdapter{
	
	@Override
	public void processCompleted(ActivitiEvent event) {
		SessionFactory factory = SessionFactoryLocal.get();
		SubHistoryBillsMapper mapper = factory.getMapper(SubHistoryBillsMapper.class);
		SubHistoryBillsBean subBill = mapper.findByProcessInstanceId(event.getProcessInstanceId());
		if(subBill != null) {
			mapper.updateStatusByProcessInstanceId(event.getProcessInstanceId(), 2);
		}else {
			HistoryBillsMapper billMapper = factory.getMapper(HistoryBillsMapper.class);
			HistoryBillsBean bill = billMapper.findByProcessInstanceId(event.getProcessInstanceId());
			billMapper.updateStatus(bill.getBillId(), 2);
			billMapper.updateEndTime(bill.getBillId(), new Date());
		}
	}
}
